//url
const BASE_URL = 'http://localhost:8080';
// const BASE_URL = 'http://2cf7881a.r15.cpolar.top';

export default {
    request(type = 'GET', url = '', date = {}, header = {}) {
        return new Promise((resolve, reject) => {
            const token = uni.getStorageSync('token');
            uni.request({
                method: type,
                url: BASE_URL + url,
                data: date,
                header: {
                    'token': token,
                    ...header
                },
                dataType: 'json',
            }).then((response) => {
                // setTimeout(function () {
                //     uni.hideLoading();
                // }, 200);
                // let [error, res] = response;
                // resolve(res.data);
                resolve(response)
            }).catch(error => {
                // let [err, res] = error;
                reject(error)
            })
        });
    },

    get(url = '', date = {}, header = {}) {
        return this.request('GET', url, date, header)
    },
    post(url = '', date = {}, header = {}) {
        return this.request('POST', url, date, header)
    },
    put(url = '', date = {}, header = {}) {
        return this.request('PUT', url, date, header)
    },
    delete(url = '', date = {}, header = {}) {
        return this.request('DELETE', url, date, header)
    },
    updateFile(url = '',
               fileName = '',
               tempFilePaths = '',
               header = {},) {
        return new Promise((resolve, reject) => {
            const token = uni.getStorageSync('token');
            // 将选择的图片上传到服务器
            uni.uploadFile({
                url: BASE_URL + url, // 上传图片的接口地址
                filePath: tempFilePaths,
                name: fileName, // 服务器接收文件的字段名
                header: {
                    'token': token,
                    ...header
                },
            }).then((response) => {
                resolve(response)
            }).catch(error => {
                reject(error)
            });
        });
    },

}
